<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Minim : : GranulateRandom</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="stylesheet.css" rel="stylesheet" type="text/css">
</head>
<body> 
<center>
<table class="mainTable">
  <tr>
    <td class="header">
    	<span class="indexheader">Minim</span><br/>
    	<span class="indexnavigation">
    		<a href="index.html">core</a> | 
    		<a href="index_ugens.html">ugens</a> | 
    		<a href="index_analysis.html">analysis</a>
    	</span>
    </td>
    <td class="border-left">&nbsp;</td>
  </tr>
  
  <tr>
    <td class="classNavigation">
    	<p class="mainTextName">GranulateRandom</p>
    	
       <p class="linkListHeader">Fields</p>
       <p class="linkList">
           
    <a href="granulaterandom_field_audio.html" title="The default input is "audio."">audio</a><br/>
    
    <a href="granulaterandom_field_fadelenmax.html" title="Controls the maximum length of the fade in and fade out.">fadeLenMax</a><br/>
    
    <a href="granulaterandom_field_fadelenmin.html" title="Controls the minimum length of the fade in and fade out.">fadeLenMin</a><br/>
    
    <a href="granulaterandom_field_grainlenmax.html" title="Controls the maximum length of each grain.">grainLenMax</a><br/>
    
    <a href="granulaterandom_field_grainlenmin.html" title="Controls the minimum length of each grain.">grainLenMin</a><br/>
    
    <a href="granulaterandom_field_spacelenmax.html" title="Controls the maximum space between each grain.">spaceLenMax</a><br/>
    
    <a href="granulaterandom_field_spacelenmin.html" title="Controls the minimum space between each grain.">spaceLenMin</a><br/>
    
	   </p>
   
    	
       <p class="linkListHeader">Methods</p>
       <p class="linkList">
           
    <a href="granulaterandom_method_channelcount.html" title="Returns the number of channels this UGen has been configured to generate.">channelCount ( )</a><br/>
    
    <a href="granulaterandom_method_getlastvalues.html" title="Return the last values generated by this UGen. This will most often be
 used by sub-classes when pulling data from their inputs.">getLastValues ( )</a><br/>
    
    <a href="granulaterandom_method_patch.html" title="Send the output of this UGen to another UGen, UGenInput, or AudioOutput.">patch ( )</a><br/>
    
    <a href="granulaterandom_method_printinputs.html" title="Prints all inputs connected to this UGen (for debugging)">printInputs ( )</a><br/>
    
    <a href="granulaterandom_method_samplerate.html" title="Returns the sample rate of this UGen.">sampleRate ( )</a><br/>
    
    <a href="granulaterandom_method_setallparameters.html" title="Immediately sets all public class members to new values.">setAllParameters ( )</a><br/>
    
    <a href="granulaterandom_method_setalltimeparameters.html" title="Immediately sets all public class members concerning time to new values.">setAllTimeParameters ( )</a><br/>
    
    <a href="granulaterandom_method_setchannelcount.html" title="Let this UGen know how many channels of audio you will be asking it for.">setChannelCount ( )</a><br/>
    
    <a href="granulaterandom_method_setsamplerate.html" title="Set the sample rate for this UGen.">setSampleRate ( )</a><br/>
    
    <a href="granulaterandom_method_tick.html" title="Generates one sample frame for this UGen.">tick ( )</a><br/>
    
    <a href="granulaterandom_method_unpatch.html" title="Unpatch this UGen from an AudioOutput or other UGen.">unpatch ( )</a><br/>
    
	   </p>
   
    </td>
    <td class="mainText border-left">
    	GranulateRandom is randomly varying version of GranulateSteady.
 Rather than have fixed values for grain length, space length, 
 and fade length, it has min and max values for each so that each 
 grain is different from the last.
    	<p class="memberSectionHeader">Constructors</p>
    	<pre><em>Constructor for GranulateRandom.
 grainLengthMin, minimum grain length of each grain, defaults to 10 msec. 
 spaceLengthMin, minimum space between each grain, defaults to 20 msec.
 fadeLengthMin, minimum length of the linear fade in and fade out of the i
 grain envelope, defaults to 2.5 msec.
 grainLengthMax, maximum grain length of each grain, defaults to 100 msec.  
 spaceLengthMax, maximum space between each grain, defaults to 200 msec.
 fadeLengthMax, maximum length of the linear fade in and fade out of the
 grain envelope, defaults to 25 msec.
 
 minAmp, minimum amplitude of the envelope, defaults to 0.
 maxAmp, maximum amplitude of the envelope, defaults to 1.</em>
GranulateRandom()
<em>Constructor for GranulateRandom.
 minAmp, minimum amplitude of the envelope, defaults to 0.
 maxAmp, maximum amplitude of the envelope, defaults to 1.</em>
GranulateRandom(float grainLengthMin, float spaceLengthMin, float fadeLengthMin, float grainLengthMax, float spaceLengthMax, float fadeLengthMax)
<em>Constructor for GranulateRandom</em>
GranulateRandom(float grainLengthMin, float spaceLengthMin, float fadeLengthMin, float grainLengthMax, float spaceLengthMax, float fadeLengthMax, float minAmp, float maxAmp)
</pre>
    	
   <p class="memberSectionHeader">Parameters</p>
   
        <span class="parameterName">grainLengthMin</span>&nbsp;&mdash;&nbsp;<span class="parameterDescription">float: minimum grain length of each grain in seconds</span><br/>
    
        <span class="parameterName">spaceLengthMin</span>&nbsp;&mdash;&nbsp;<span class="parameterDescription">float: minimum space between each grain in seconds</span><br/>
    
        <span class="parameterName">fadeLengthMin</span>&nbsp;&mdash;&nbsp;<span class="parameterDescription">float: minimum length of the linear fade in and fade out of the grain envelope in seconds</span><br/>
    
        <span class="parameterName">grainLengthMax</span>&nbsp;&mdash;&nbsp;<span class="parameterDescription">float: maximum grain length of each grain in seconds</span><br/>
    
        <span class="parameterName">spaceLengthMax</span>&nbsp;&mdash;&nbsp;<span class="parameterDescription">float: maximum space between each grain in seconds</span><br/>
    
        <span class="parameterName">fadeLengthMax</span>&nbsp;&mdash;&nbsp;<span class="parameterDescription">float: maximum length of the linear fade in and fade out of the grain envelope in seconds</span><br/>
    
        <span class="parameterName">minAmp</span>&nbsp;&mdash;&nbsp;<span class="parameterDescription">float: minimum amplitude of the envelope</span><br/>
    
        <span class="parameterName">maxAmp</span>&nbsp;&mdash;&nbsp;<span class="parameterDescription">float: maximum amplitude of the envelope</span><br/>
    
   
    	<p class="memberSectionHeader">Related</p>
    	<A href="granulatesteady_class_granulatesteady.html">GranulateSteady</A><BR>
<A href="ugen_class_ugen.html">UGen</A><BR>

    	<p class="memberSectionHeader">Example</p>
    	<pre>/* granulateRandomExample&lt;br/>
   is an example of using the GranulateRandom UGen inside an instrument.
   The GranulateRandom UGen is basically an amplitude modulation of the
   incoming audio, which turns on for a random amount of time and then off
   again for a random amount of time.  If one uses several of these concurrently
   it produces the "grain cloud" typical of granular synthesis.
   &lt;p>
   For more information about Minim and additional features, 
   visit http://code.compartmental.net/minim/
   &lt;p>
   author: Anderson Mills&lt;br/>
   Anderson Mills's work was supported by numediart (www.numediart.org)
*/

// import everything necessary to make sound.
import ddf.minim.*;
import ddf.minim.ugens.*;

// create all of the variables that will need to be accessed in
// more than one methods (setup(), draw(), stop()).
Minim minim;
AudioOutput out;

// The GranularInstrument is intended use GranulateRandom to chop up audio
//    generated using a triangle wave
//    into sound grains which are random in length and spaced randomly within the
//    parameters given.  The beginning frequency and the ending frequency of the 
//    tone is set in the constructor of the instrument.

// Every instrument must implement the Instrument interface so 
// playNote() can call the instrument's methods.
class GrabularInstrument implements Instrument
{
  // create all variables that must be used throughout the class
  Line freqLine;
  GranulateRandom chopper;
  
  // the constructor for this instrument.  The amplitude and beginning and ending
  // frequency must all be specified.
  GrabularInstrument( float amplitude, float begFreq, float endFreq )
  {
    // create new instances of any UGen objects necessary
    // the tone
    Oscil toneOsc = new Oscil( begFreq, amplitude, Waves.TRIANGLE);
    // a line to specify the frequency of the tone
    freqLine = new Line( 2.6, begFreq, endFreq );
    // the granulation of the tone
    chopper = new GranulateRandom( 0.005, 0.005, 0.001, 0.020, 0.020, 0.002 );

    // patch everything together up until the output
    // the frequency line goes to the toneOsc frequency
    freqLine.patch( toneOsc.frequency );
    // and the tone goes into the chopper
    toneOsc.patch( chopper );
  }
  
  // every instrument must have a noteOn( float ) method
  void noteOn( float dur )
  {
    // change the time of the line to the duration minus 1 second
    freqLine.setLineTime( dur - 1.0 );
    // activate the line
    freqLine.activate();
    // and patch the chopper to the output to get the ball rolling
    chopper.patch( out );
  }
 
  // every instrument must have a noteOff() method 
  void noteOff()
  {
    // pull the chopper off the output
    // an ADSR here would be nicer, but this is the dirty way
    chopper.unpatch( out );
  }
}

// setup is run once at the beginning
void setup()
{
  // initialize the drawing window
  size(512, 200, P2D);

  // initialize the minim and out objects
  minim = new Minim(this);
  out = minim.getLineOut(Minim.MONO, 2048);
  // the note offsets are calculated because this makes composition easier
  
  // because there are so many calls to the granularInstrument, it becomes easier
  // to compose here by using variables for note parameters.  
  float myOffset = 0.5;
  float noteLen = 0.0;
  out.setNoteOffset( myOffset );
  
  // this starts with three GroanularInstruments all taking the same path
  float myTargetFreq = 660.9f;
  float vol = 0.2;
  noteLen = 4.5;
  out.playNote( 0.0, noteLen, new GrabularInstrument( vol, 440.0f, myTargetFreq ) );
  out.playNote( 0.1, noteLen, new GrabularInstrument( vol, 440.0f, myTargetFreq ) );
  out.playNote( 0.2, noteLen, new GrabularInstrument( vol, 440.0f, myTargetFreq ) );

  // set up the offset for the next note
  myOffset += noteLen;
  out.setNoteOffset( myOffset );
  
  // The next "note" will be nine GranularInstruments headed from the previous myTargetFreq
  // to the first 9 harmonics of 220Hz (220, 440, 660, ... )
  vol = 0.15;
  noteLen = 7.0;
  int nFreqs = 9;
  float myFreqs[] = new float[ nFreqs ];
  for ( int i=0; i&lt;nFreqs; i++ )
  {
    //myFreqs[ i ] = random( 900 ) + 100.0;
    myFreqs[ i ] = i*220.0;
  }
  out.playNote( 0.00, noteLen, new GrabularInstrument( vol, myTargetFreq, myFreqs[ 0 ] ) );
  out.playNote( 0.05, noteLen, new GrabularInstrument( vol, myTargetFreq, myFreqs[ 1 ] ) );
  out.playNote( 0.10, noteLen, new GrabularInstrument( vol, myTargetFreq, myFreqs[ 2 ] ) );
  out.playNote( 0.15, noteLen, new GrabularInstrument( vol, myTargetFreq, myFreqs[ 3 ] ) );
  out.playNote( 0.20, noteLen, new GrabularInstrument( vol, myTargetFreq, myFreqs[ 4 ] ) );
  out.playNote( 0.25, noteLen, new GrabularInstrument( vol, myTargetFreq, myFreqs[ 5 ] ) );
  out.playNote( 0.30, noteLen, new GrabularInstrument( vol, myTargetFreq, myFreqs[ 6 ] ) );
  out.playNote( 0.35, noteLen, new GrabularInstrument( vol, myTargetFreq, myFreqs[ 7 ] ) );
  out.playNote( 0.40, noteLen, new GrabularInstrument( vol, myTargetFreq, myFreqs[ 8 ] ) );

  // set up the next note offset
  myOffset += noteLen;
  out.setNoteOffset( myOffset );
  
  // The next note will be 9 GranularInstruments going from their previous values to a
  // new myTargetFreq.
  myTargetFreq = 1320.9f;
  vol = 0.15;
  noteLen = 7.0;
  out.playNote( 0.05, noteLen, new GrabularInstrument( vol, myFreqs[ 0 ], myTargetFreq ) );
  out.playNote( 0.10, noteLen, new GrabularInstrument( vol, myFreqs[ 1 ], myTargetFreq ) );
  out.playNote( 0.15, noteLen, new GrabularInstrument( vol, myFreqs[ 2 ], myTargetFreq ) );
  out.playNote( 0.20, noteLen, new GrabularInstrument( vol, myFreqs[ 3 ], myTargetFreq ) );
  out.playNote( 0.25, noteLen, new GrabularInstrument( vol, myFreqs[ 4 ], myTargetFreq ) );
  out.playNote( 0.30, noteLen, new GrabularInstrument( vol, myFreqs[ 5 ], myTargetFreq ) );
  out.playNote( 0.35, noteLen, new GrabularInstrument( vol, myFreqs[ 6 ], myTargetFreq ) );
  out.playNote( 0.40, noteLen, new GrabularInstrument( vol, myFreqs[ 7 ], myTargetFreq ) );
  out.playNote( 0.45, noteLen, new GrabularInstrument( vol, myFreqs[ 8 ], myTargetFreq ) );

 }

// draw is run many times
void draw()
{
  // erase the window to dark green
  background( 0, 64, 32 );
  // draw using a purple stroke
  stroke( 232, 0, 255 );
  // draw the waveforms
  for( int i = 0; i &lt; out.bufferSize() - 1; i++ )
  {
    // find the x position of each buffer value
    float x1  =  map( i, 0, out.bufferSize(), 0, width );
    float x2  =  map( i+1, 0, out.bufferSize(), 0, width );
    // draw a line from one buffer position to the next for both channels
    line( x1, 50 + out.left.get(i)*50, x2, 50 + out.left.get(i+1)*50);
    line( x1, 150 + out.right.get(i)*50, x2, 150 + out.right.get(i+1)*50);
  }  
}

</pre>
    	<p class="memberSectionHeader">Usage</p>
    	Web & Application
    </td>
  </tr>
</table>
</center>
</body>
</html>